<?php
namespace app\models;

class Util extends \yii\base\Model {

	/**
	 * 当前时间
	 *
	 * @return string
	 */
	public static function now() {
		return date('YmdHis');
	}

	/**
	 * 格式化时间
	 * 年Y 月m 日d 时H 分i 秒s
	 */
	public static function date_format($dateTime, $format = 'Y-m-d H:i:s') {
		if ($dateTime == '') {
			return '';
		}
		$timestamp = Util::getDateStamp($dateTime);
		return date($format, $timestamp);
	}

	/**
	 * 时间增加
	 */
	public static function dateAdd($interval, $number, $date) {
		$year = substr($date, 0, 4);
		$month = substr($date, 4, 2);
		$day = substr($date, 6, 2);
		$hour = substr($date, 8, 2);
		$minute = substr($date, 10, 2);
		$second = substr($date, 12, 2);
		switch ($interval) {
		case "Y":
			$year += $number;
			break;
		case "m":
			$month += $number;
			break;
		case "d":
			$day += $number;
			break;
		case "H":
			$hour += $number;
			break;
		case "i":
			$minute += $number;
			break;
		case "s":
			$second += $number;
			break;
		}
		$timestamp = mktime($hour, $minute, $second, $month, $day, $year);
		return date('YmdHis', $timestamp);
	}

	/**
	 * 返回日期时间戳
	 */
	public static function getDateStamp($dateTime) {
		$year = substr($dateTime, 0, 4);
		$month = substr($dateTime, 4, 2);
		$day = substr($dateTime, 6, 2);
		if (count($dateTime) == 8) {
			$hour = 0;
			$minute = 0;
			$second = 0;
		} else {
			$hour = substr($dateTime, 8, 2);
			$minute = substr($dateTime, 10, 2);
			$second = substr($dateTime, 12, 2);
		}
		return mktime($hour, $minute, $second, $month, $day, $year);
	}

	/**
	 * 导出文件
	 * @param PHPExcel $excel
	 * @param string $filename
	 */
	public static function export(&$excel, $filename) {
		header('Cache-Control: no-cache');
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="' . $filename . '.xls"');
		
		$excelWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel5');
		$excelWriter->save('php://output');
	}

	/**
	 * GET请求数据
	 * @param string $url
	 */
	public static function get($url) {
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
		curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
		curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
		                                            // curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
		                                            // curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
		curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
		curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
		$output = curl_exec($curl);
		curl_close($curl);
		/*
		 * file_put_contents('R:/weixin.txt', "weixin_api==========>\r\n", FILE_APPEND);
		 * file_put_contents('R:/weixin.txt', $url . "\r\n", FILE_APPEND);
		 * file_put_contents('R:/weixin.txt', $output . "\r\n", FILE_APPEND);
		 */
		return $output;
	}

	/**
	 * POST请求数据
	 * @param string $url
	 * @param string $data
	 * @return string
	 */
	public static function post($url, $data) {
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_POST, 1);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
		curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
		curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
		                                            // curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
		                                            // curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
		curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
		curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
		$output = curl_exec($curl);
		curl_close($curl);
		return $output;
	}
}